// 记录当前选择的单位
library SelectUnit initializer init

    globals
        unit array NOW_UNIT
        // 双击计时器(有效时间1小时)
        timer double_timer = CreateTimer()
        real array double_timer_i
    endglobals

    function SetVarUnit takes nothing returns nothing
        local integer uid = GetPlayerId(GetTriggerPlayer())

        // 模拟双击
        if GetTriggerUnit() == NOW_UNIT[uid] and (TimerGetElapsed(double_timer) - double_timer_i[uid] <= 0.5) then
            // call BJDebugMsg("双击单位：" + GetUnitName(NOW_UNIT[uid]))
        endif
        set double_timer_i[uid] = TimerGetElapsed(double_timer)
        set NOW_UNIT[uid] = GetTriggerUnit()
        // call BJDebugMsg("选择单位名称：" + GetUnitName(NOW_UNIT[uid]))

    endfunction

    // 取消选择
    function ClearSetVarUnit takes nothing returns nothing
        local integer uid = GetPlayerId(GetTriggerPlayer())
        
        set NOW_UNIT[uid] = null
    endfunction
 
    private function init takes nothing returns nothing
        local trigger trig = CreateTrigger()

        call TriggerRegisterPlayerUnitEvent(trig, Player(0), EVENT_PLAYER_UNIT_SELECTED, null)
        call TriggerRegisterPlayerUnitEvent(trig, Player(1), EVENT_PLAYER_UNIT_SELECTED, null)
        call TriggerRegisterPlayerUnitEvent(trig, Player(2), EVENT_PLAYER_UNIT_SELECTED, null)
        call TriggerRegisterPlayerUnitEvent(trig, Player(3), EVENT_PLAYER_UNIT_SELECTED, null)
        call TriggerAddAction(trig, function SetVarUnit)

        set trig = CreateTrigger()
        call TriggerRegisterPlayerUnitEvent(trig, Player(0), EVENT_PLAYER_UNIT_DESELECTED, null)
        call TriggerRegisterPlayerUnitEvent(trig, Player(1), EVENT_PLAYER_UNIT_DESELECTED, null)
        call TriggerRegisterPlayerUnitEvent(trig, Player(2), EVENT_PLAYER_UNIT_DESELECTED, null)
        call TriggerRegisterPlayerUnitEvent(trig, Player(3), EVENT_PLAYER_UNIT_DESELECTED, null)
        call TriggerAddAction(trig, function ClearSetVarUnit)

        set trig = null

        call TimerStart(double_timer, 3600.0, false, null)

        //禁止框选(取消选择多个单位的功能)
        call EnableDragSelect(false,true)
    endfunction
endlibrary
